﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Pivot;
using System.Windows.Data;
using Microsoft.Internal.Pivot.Views;
using Microsoft.Internal.Pivot.Controls;
using System.Windows.Media;

namespace AdaptPivotViewerNameSpace
{
    public class AdaptPivotViewer : PivotViewer
    {
        /// <summary>
        /// The DataTemplate to define what you want to appear in the detail area
        /// </summary>
        public DataTemplate ViewTemplate { get; set; }
        private Button B2 { get; set; }
        public GridLength DPWidth { get { return (((Border)(((CollectionViewerView)((Grid)this.GetTemplateChild("PART_Container")).Children[2]).Content as Grid).Children[1]).Child as Grid).ColumnDefinitions[2].Width; } set { (((Border)(((CollectionViewerView)((Grid)this.GetTemplateChild("PART_Container")).Children[2]).Content as Grid).Children[1]).Child as Grid).ColumnDefinitions[2].Width = value; } }

        /// <summary>
        /// Accessor to the content control
        /// </summary>
        public Control Content { get; set; }

        /// <summary>
        /// The ItemsSource property on the DataTemplate control to bind the PivotViewer's InScopeItems to
        /// </summary>
        public string ItemsSourcePropertyName { get; set; }

        /// <summary>
        /// Renders the display
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var partContainer = (Grid)this.GetTemplateChild("PART_Container");
            var zoomSlider = (ClickDragSlider)this.GetTemplateChild("PART_ZoomSlider");
            var controlBar = ((Grid)partContainer.Children[1]);
            var collectionViewerView = ((CollectionViewerView)partContainer.Children[2]);
            var container = collectionViewerView.Content as Grid;
            var viewerGrid = ((Border)container.Children[1]).Child as Grid;
            var collectionViewContainer = (CollectionViewContainer)viewerGrid.Children[0];


            // Add an element into the control bar 
            B2 = new Button() { Background = new SolidColorBrush(Color.FromArgb(255,204,204,204)), Content="Lst", Width = 30, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, Style=Application.Current.Resources["GrayButtonStyle"] as Style };

            B2.MouseEnter += (sender, eargs) => { (sender as Button).Style = Application.Current.Resources["AquaButtonStyle"] as Style; };
            B2.MouseLeave += (sender, eargs) => { if (!ShowingList) (sender as Button).Style = Application.Current.Resources["GrayButtonStyle"] as Style; };


            B2.Click += (sender, eargs) => {    if (ShowingList) { hideDataGrid(); }
                                                else { showDataGrid(); } };

            B2.Margin = new Thickness(0,0,96,0);
            B2.Height = 20;
            Grid.SetColumn(B2, 2); // 2 = view buttons 
            Grid.SetRow(B2, 0);
            controlBar.Children[0].Visibility = Visibility.Collapsed; // rectangle 
            controlBar.Children.Add(B2);

            this.ViewChanged += (sender, eargs) => { if (this.ShowingList) this.hideDataGrid(); };
        }

        private GridLength oldWidth = new GridLength(0);
        public bool ShowingList = false;
        public void showDataGrid()
        {
            if (ShowingList)
                return;

            ShowingList = true;

            B2.Style = Application.Current.Resources["AquaButtonStyle"] as Style;
            
            var partContainer = (Grid)this.GetTemplateChild("PART_Container");
            var zoomSlider = (ClickDragSlider)this.GetTemplateChild("PART_ZoomSlider");
            var collectionViewerView = ((CollectionViewerView)partContainer.Children[2]);
            var container = collectionViewerView.Content as Grid;
            var viewerGrid = ((Border)container.Children[1]).Child as Grid;
            var detailView = (Grid)viewerGrid.Children[4];

            zoomSlider.IsHitTestVisible = false;

            detailView.Children[0].Visibility = System.Windows.Visibility.Collapsed;
            Content = (Control)ViewTemplate.LoadContent();
            Grid.SetColumn(Content, 1);
            Grid.SetColumnSpan(Content, 2);
            Canvas.SetZIndex(Content, Canvas.GetZIndex(viewerGrid.Children[0]) - 1);
            viewerGrid.Children[0].Visibility = Visibility.Collapsed;
            var dp = (DependencyProperty)Content.GetType().GetField(ItemsSourcePropertyName + "Property", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy).GetValue(null);
            ((Control)Content).SetBinding(dp, new Binding("InScopeItems"));

            //Put the DataTemplate content in the PivotViewer
            //detailView.Children.Insert(0, (UIElement)content);
            viewerGrid.Children.Add(Content);
            Grid.SetColumn(Content, 1);
            Content.Margin = new Thickness(5);
            oldWidth = viewerGrid.ColumnDefinitions[2].Width;
            viewerGrid.ColumnDefinitions[2].Width = new GridLength(0);
        }

        public void hideDataGrid()
        {
            ShowingList = false;

            B2.Style = Application.Current.Resources["GrayButtonStyle"] as Style;

            var partContainer = (Grid)this.GetTemplateChild("PART_Container");
            var zoomSlider = (ClickDragSlider)this.GetTemplateChild("PART_ZoomSlider");
            var collectionViewerView = ((CollectionViewerView)partContainer.Children[2]);
            var container = collectionViewerView.Content as Grid;
            var viewerGrid = ((Border)container.Children[1]).Child as Grid;
            var detailView = (Grid)viewerGrid.Children[4];

            zoomSlider.IsHitTestVisible = true;

            detailView.Children[0].Visibility = System.Windows.Visibility.Visible;
            viewerGrid.ColumnDefinitions[2].Width = oldWidth;
            viewerGrid.Children.RemoveAt(5);
            viewerGrid.Children[0].Visibility = Visibility.Visible;
        }
    }
}
